package com.utils;

import android.util.Log;

import java.io.DataOutputStream;

public class RootUtils {
    //设置权限
    public static boolean runDevchmod(String binpath, String devpath) {
        try {
            Process su;
            su = Runtime.getRuntime().exec(binpath);
            String cmd = "chmod 666 " + devpath + "\n" + "exit\n";
            su.getOutputStream().write(cmd.getBytes());
            if (su.waitFor() != 0) {
                throw new SecurityException("securityexception");
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("MYINFO", "runDevchmod Exception e = " + e);

        }
        return true;
    }

    //root 权限
    public static boolean runRootCommand(String command) {
        Process process = null;
        DataOutputStream os = null;

        try {
            process = Runtime.getRuntime().exec("sh");// Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
            Log.d("Phone Link",
                    "su root - the device is not rooted,  error message： "
                            + e.getMessage());
            return false;
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
                if (null != process) {
                    process.destroy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}
